<?php

namespace App\Http\Controllers\Api;

use App\Models\Book;
use App\Models\GivedGift;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use App\Http\Resources\Book as BookCollection;
use App\Http\Resources\GivedGift as GivedGiftCollection;

class RankingListController extends ApiController
{

    /**
     *
     * @api {get} /ranking_list/flowers_user 获取鲜花榜
     * @apiName Get flowersUser
     * @apiGroup Gift
     *
     *
     * @apiParam {int} [limit] 获取数量
     * @apiVersion 1.0.0
     * @apiSampleRequest /api/ranking_list/flowers_user
     *
     */
    public function flowersUser(){

        $limit = Input::get('limit') ?: 20;

        $gifts = GivedGift::query()
            ->where('gift_id',1)
            ->whereHas('sendUser')
            ->groupBy('uid')
            ->select(DB::raw('*,sum(counts) as count'))
            ->orderBy('count','desc')
            ->limit($limit)
            ->get();


        return GivedGiftCollection::collection($gifts);
    }

    /**
     *
     * @api {get} /ranking_list/rewarded_user 获取荣誉榜
     * @apiName Get rewardedUser
     * @apiGroup Gift
     *
     *
     * @apiParam {int} [limit] 获取数量
     * @apiVersion 1.0.0
     * @apiSampleRequest /api/ranking_list/flowers_user
     *
     */
    public function rewardedUser(){


        $limit = Input::get('limit') ?: 20;

        $gifts = GivedGift::query()
            ->where('gift_id','!=',1)
            ->whereHas('sendUser')
            ->groupBy('uid')
            ->select(DB::raw('*,sum(counts) as count'))
            ->orderBy('count','desc')
            ->limit($limit)
            ->get();

        return GivedGiftCollection::collection($gifts);

    }





}
